package com.edu.v2.compile.parser;

import com.edu.compile.Parser;
import com.edu.compile.Config;
import com.edu.compile.util.JsonUtils;
import com.edu.compile.util.ReflectUtils;
import com.edu.model.dto.NodeMetadata;

import java.lang.reflect.Type;

/**
 * @author yanzhao
 * @date 2021/10/19 00:01
 * @since 1.0.0
 */
//public class ConfigParser implements Parser {
//public class ConfigParser implements Parser<NodeMetadata, Config> {
//public class ConfigParser<IN, OUT> implements Parser<IN, OUT> {
//public class ConfigParser<OUT> implements Parser<NodeMetadata, OUT> {
//public class ConfigParser<T, OUT> implements Parser<NodeMetadata, OUT> {
//public class ConfigParser<NodeMetadata, T, OUT> implements Parser<NodeMetadata, OUT> {
public class ConfigParser<OUT extends Config> implements Parser<NodeMetadata, OUT> {
    @Override
    public OUT parse(NodeMetadata nodeMetadata) {
        Type[] actualTypeArguments = ReflectUtils.parseParameterizedType(getClass());
        String config = nodeMetadata.getConfig();
        return (OUT) JsonUtils.parseObject(config, (Class) actualTypeArguments[1]);
    }
}
